const Product = require('../model/product.model')
const {error} = require("../constant/err.type");
class ProductsService {
    async addProducts (param, type, ctx) {
        console.log('我是参数', param, type)
        try {
            if (type && type === 'create') {
                const res = await Product.create(param)
                return res ? res.dataValues : null
            } else {
                const { productName, price, productImg, contact, productType } = param
                const obj = { productName, price, productImg, contact, productType }
                const res = await Product.update(obj, {where: {userId: param.userId, id: param.id}})
                return res ? res : null
            }
        } catch (err) {
            ctx.app.emit('error', error, ctx)
            return null
        }
    }
    async getList (pageNum, pageSize) {
        const offset = (pageNum - 1) * pageSize
        const {count, rows} = await Product.findAndCountAll({
            offset,
            limit: pageSize * 1
        })
        return {
            pageNum,
            pageSize,
            total: count,
            list: rows
        }
    }
}
module.exports = new ProductsService()
